package de.slothsoft.wasbinich.swingclient.table;

import static de.slothsoft.wasbinich.core.WasBinIch.SUPPORTED_LANGUAGES;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import de.slothsoft.commons.swing.AbstractListTableModel;
import de.slothsoft.wasbinich.core.FullPicture;
import de.slothsoft.wasbinich.swingclient.res.Messages;

public class AbstractPictureTableModel extends AbstractListTableModel<FullPicture> {

    private static final long serialVersionUID = -8327886423352142548L;

    private static final String[] COLUMNS = createColumns();
    public static final int[] COLUMNS_WIDTH = createColumnWidth(COLUMNS);
    public static final int COLUMN_FIRST_DYNAMIC_COLUMN = 0;

    private static String[] createColumns() {
	List<String> columns = new ArrayList<>();
	for (Locale supportedLanguage : SUPPORTED_LANGUAGES) {
	    columns.add(MessageFormat.format(Messages.getString("Picture.correctAnswer"),
		    supportedLanguage.getDisplayLanguage()));
	    columns.add(MessageFormat.format(Messages.getString("Picture.description"),
		    supportedLanguage.getDisplayLanguage()));
	}
	return columns.toArray(new String[columns.size()]);
    }

    private static int[] createColumnWidth(String[] columns) {
	int[] result = new int[columns.length];
	for (int i = 0; i < result.length; i++) {
	    result[i] = 100;
	}
	return result;
    }

    public AbstractPictureTableModel() {
	super(COLUMNS);
    }

    @Override
    protected Object getProperty(FullPicture value, int column) {
	switch (column) {
	default:
	    int dynamicColumn = column - COLUMN_FIRST_DYNAMIC_COLUMN;
	    if (dynamicColumn % 2 == 0)
		return value.getCorrectAnswers(SUPPORTED_LANGUAGES[dynamicColumn / 2]);
	    return value.getDescription(SUPPORTED_LANGUAGES[dynamicColumn / 2]);
	}
    }

}
